﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Carwash.CW_Storage;

namespace Carwash.CW_Security
{   

    class CW_Security
    {
        #region variables
            private int pinAttempts = MAX_NUM_PIN_ATTEMPTS;
            private int passwordAttempts = MAX_NUM_PASSWORD_ATTEMPTS;
            private static CW_Security _securityInstance= null;
            private string[] loginData;
        #endregion

        #region constants
            private const int MANAGER = 1;
            private const int OPERATOR = 2;
            private const int DELUXE = 3;
            private const int SUPER = 4;
            private const int ULTRA = 5;
            private const int INVALID_PIN = 0;
            private const int MAX_NUM_PIN_ATTEMPTS = 8;
            private const int MAX_NUM_PASSWORD_ATTEMPTS = 5;
        #endregion

        private CW_Security()
        {
        }

        /// <summary>
        /// Singleton Pattern. Method creates new instance for
        /// the first time or retrieves the existing instance
        /// </summary>
        /// <returns>returns unique intance of the CW_Security</returns>
        public static CW_Security getInstance()
        {
            if (null == _securityInstance)
            {
                _securityInstance = new CW_Security();
            }
            return _securityInstance;
        }

        /// <summary>
        /// Method verifies whether the entered password is correct
        /// </summary>
        /// <param name="password"></param>
        /// <returns>returns whether the entered password is correct</returns>
        public bool validatePassword(int pin, int password)
        {
            bool returnValue = true;
            if (loginData == null)
            {
                returnValue = false;
            }
            else
            {
                if (Convert.ToInt32(loginData[0]) != pin)
                {
                    returnValue = false;
                }
                if (Convert.ToInt32(loginData[2]) != password)
                {
                    returnValue = false;
                }
            }
            if (returnValue)
            {
                passwordAttempts = MAX_NUM_PASSWORD_ATTEMPTS;
            }
            else
            {
                passwordAttempts--;
            }
            return returnValue;
        }

        public bool maxNumPasswordAttemptsExceeded()
        {
            return (passwordAttempts <= 0);
        }

        public void resetPasswordAttempts()
        {
            passwordAttempts = MAX_NUM_PASSWORD_ATTEMPTS;
        }

        /// <summary>
        /// method retrieves the type of pin associated with this PIN
        /// </summary>
        /// <param name="pin"></param>
        /// <returns>returns the type of PIN associated with this pin</returns>
        public int validatePin(int pin)
        {
            CW_Storage_Facade storage = new CW_Storage_Facade();
            string[] pinData = storage.getPINData(pin);
            loginData = pinData;
            if (pinData == null)
            {
                pinAttempts--;
                return INVALID_PIN;
            }
            else
            {
                pinAttempts = MAX_NUM_PIN_ATTEMPTS;
                return (Convert.ToInt32(pinData[1]));
            }
        }

        public bool maxNumPinAttemptsExceeded()
        {
            return (pinAttempts <= 0);
        }

        public void resetPinAttempts()
        {
            pinAttempts = MAX_NUM_PIN_ATTEMPTS;
        }

        /// <summary>
        /// method checks if the password has the appropriate length
        /// </summary>
        /// <param name="password"></param>
        /// <returns>returns whether the entered password has an appropriate length</returns>
        public bool verifyPasswordLength(string password)
        {
            return false;
        }

        public int getPinAttempts()
        {
            return pinAttempts;
        }

        public int getPasswordAttempts()
        {
            return passwordAttempts;
        }
    }
}
